package itbour.onetouchshow.utils.aliyun;

import java.text.SimpleDateFormat;
import java.util.Random;

/**
 * Created by zjl_d on 2016/6/28.
 */
public class OSSFileName {
//    [typeStr]/[idStr]/[YYYY]/[MM]/[DD]/[HHmmssSSS]_[random(20)]
//            [typeStr]: "image"或"audio"或"video"
//            [idStr]: "U"+userId或"T"+random(6)
//            [YYYY]/[MM]/[DD]: 年/月/日
//    [random(20)]: 20位随机字符串
//    例如:
//    文件名:
//    image/U1055/2016/05/131/171249525_Qkssf0RdReDhxVAm1LxV
//    对应下载链接:
//    http://itbour-user.oss-cn-hangzhou.aliyuncs.com/image/U1055/2016/05/131/171249525_Qkssf0RdReDhxVAm1LxV


    public static String line = "_";
    public static String u = "U";
    public static String li = "/";

    public static String createFileName(String type, String userId) {
        String fileName = type + li + u + userId + li + time() + line + mathRandom();
        return fileName;
    }


    /**
     * 获取系统时间
     *
     * @return
     */
    private static String time() {
        long currentTimeMillis = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss/SSS");
        String format = sdf.format(currentTimeMillis);
        return format;
    }

    private static String mathRandom() {
        Random random = new Random();
        String[] lenth = new String[]{
                "o", "p", "a", "H", "J", "K", "s", "d", "f",
                "g", "h", "j", "k", "l", "z",
                "0", "1", "1", "C", "V", "B",
                "9", "q", "w", "e", "r", "t",
                "y", "u", "i", "x", "c", "v",
                "b", "n", "m", "Q", "W", "E",
                "R", "T", "Y", "U", "I", "O",
                "P", "A", "S", "6", "7", "8",
                "D", "F", "G", "3", "4", "5",
                "L", "Z", "X", "N", "M"};
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 20; i++) {
            int s = random.nextInt(lenth.length);
            stringBuffer.append(lenth[s]);
        }

        return stringBuffer.toString();
    }

}
